package com.weichen.studentgameexchange.common.common;

import lombok.Data;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
 * 功能：
 * 作者：惟尘
 * 日期：2025/1/28 12:30
 */
@Data
@Component
public class StringToDate {

    private String date;


    public LocalDate strToDate(String s) {
        LocalDate localDate = null;
        // 定义日期时间格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        try {
            // 将字符串转换为LocalDateTime
            LocalDateTime localDateTime = LocalDateTime.parse(s, dateTimeFormatter);
            localDate = localDateTime.toLocalDate();
            return localDate;
        } catch (DateTimeParseException e) {
            // 处理日期时间解析异常
            System.err.println("日期时间字符串格式不正确: " + e.getMessage());
        }
        return localDate;
    }

}
